package com.zw.yzk.kabu.data.repository;

import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.cache.store.CosplayStore;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.CosplayListEntity;
import com.zw.yzk.kabu.data.network.service.CosplayListService;
import com.zw.yzk.kabu.data.network.task.CosplayListTask;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2017/1/4.
 */

public class CosplayListRepository extends CommonRepository {

    @Inject
    CosplayStore cosplayStore;
    private boolean refresh;
    private int dataType;
    private int totalCount;
    private int concernedCount;
    private int recommendCount;

    @Inject
    public CosplayListRepository() {
        totalCount = CosplayListTask.DEFAULT_COS_COUNT_PER_PAGE;
        concernedCount = CosplayListTask.DEFAULT_COS_COUNT_PER_PAGE;
        recommendCount = CosplayListTask.DEFAULT_COS_COUNT_PER_PAGE;
    }

    @Override
    protected Observable commonObserve(Task task) {
        if (((CosplayListTask) task).getLocal) {
            return Observable.concat(Observable.just(cosplayStore.getCosplayList()), super.commonObserve(task));
        } else {
            return super.commonObserve(task);
        }

    }

    @Override
    protected Observable connectServer(Task task) {
        CosplayListService cosplayListService = ServiceGenerator.createService(CosplayListService.class);
        CosplayListTask cosplayListTask = (CosplayListTask) task;

        dataType = cosplayListTask.type;
        refresh = cosplayListTask.start == 0;
        switch (cosplayListTask.type) {
            case CosplayListTask.TYPE_ALL:
                if (cosplayListTask.start >= totalCount) {
                    CosplayListEntity entity = new CosplayListEntity();
                    entity.errCode = Constant.RESULT_OK;
                    entity.result.totalCount = totalCount;

                    return Observable.just(entity);
                } else {
                    return cosplayListService.getCosplayList(
                            cosplayListTask.lastId,
                            cosplayListTask.size,
                            cosplayListTask.type,
                            cosplayListTask.userId,
                            cosplayListTask.accessToken);
                }
            case CosplayListTask.TYPE_CONCERNED:
                if (cosplayListTask.start >= concernedCount) {
                    CosplayListEntity entity = new CosplayListEntity();
                    entity.errCode = Constant.RESULT_OK;
                    entity.result.totalCount = concernedCount;

                    return Observable.just(entity);
                } else {
                    return cosplayListService.getCosplayList(
                            cosplayListTask.lastId,
                            cosplayListTask.size,
                            cosplayListTask.type,
                            cosplayListTask.userId,
                            cosplayListTask.accessToken);
                }
            case CosplayListTask.TYPE_RECOMMEND:
                if (cosplayListTask.start >= recommendCount) {
                    CosplayListEntity entity = new CosplayListEntity();
                    entity.errCode = Constant.RESULT_OK;
                    entity.result.totalCount = recommendCount;

                    return Observable.just(entity);
                } else {
                    return cosplayListService.getCosplayList(
                            cosplayListTask.lastId,
                            cosplayListTask.size,
                            cosplayListTask.type,
                            cosplayListTask.userId,
                            cosplayListTask.accessToken);
                }
            default:
                return cosplayListService.getCosplayList(
                        cosplayListTask.lastId,
                        cosplayListTask.size,
                        cosplayListTask.type,
                        cosplayListTask.userId,
                        cosplayListTask.accessToken);
        }
    }

    @Override
    protected Func1<CosplayListEntity, Observable> getServerData() {
        return new Func1<CosplayListEntity, Observable>() {
            @Override
            public Observable call(CosplayListEntity cosplayListEntity) {
                switch (dataType) {
                    case CosplayListTask.TYPE_ALL:
                        totalCount = cosplayListEntity.result.totalCount;
                        break;
                    case CosplayListTask.TYPE_CONCERNED:
                        concernedCount = cosplayListEntity.result.totalCount;
                        break;
                    case CosplayListTask.TYPE_RECOMMEND:
                        recommendCount = cosplayListEntity.result.totalCount;
                        break;
                    default:
                        break;
                }
                saveCosList(refresh, cosplayListEntity);
                return Observable.just(cosplayListEntity);
            }
        };
    }

    /**
     * 保存本地列表
     *
     * @param refresh 是否是属于刷新操作
     * @param entity  cos列表
     */
    private void saveCosList(boolean refresh, CosplayListEntity entity) {
        if (refresh) {
            //是刷新操作，需要刷新数据库
            cosplayStore.refreshCosplayList(entity);
        } else {
            //不是刷新操作，直接添加数据到数据库
            cosplayStore.addCosplayList(entity);
        }
    }
}
